package com.base.cn.platform.os.controller.op.groom;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.groom.OpGroomData;
import com.base.cn.platform.os.entity.op.groom.OpGroomPlace;
import com.base.cn.platform.os.service.op.groom.OpGroomDataBiz;
import com.base.cn.platform.os.service.op.groom.OpGroomPlaceBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 推荐数据Controller
 *
 * @author ll
 * @create 2018-05-07
 */
@RestController
@RequestMapping("/manage/op")
public class OpGroomDataController extends BaseController {


    @Autowired
    private OpGroomDataBiz opGroomDataBiz;
    @Autowired
    private OpGroomPlaceBiz opGroomPlaceBiz;
    @Autowired
    private CustomCacheChannel customCacheChannel;
    /**
     * 单个推荐位置添加推荐数据
     * @param dataIds
     * @param dataType
     * @param placeKey
     * @param groomNum
     * @return
     */
    @RequestMapping("/saveUpdateOpGroomData")
    public Map<String,Object> saveUpdateOpGroomData(@RequestParam("dataIds")String dataIds,
                                                    @RequestParam("dataType")String dataType,
                                                    @RequestParam("placeKey")String placeKey,
                                                    @RequestParam("groomNum")Integer groomNum,
                                                    @RequestParam("dataSorts")String dataSorts){
        if(StringUtils.isEmpty(dataIds)){
            dataIds ="0";
        }
        if(StringUtils.isEmpty(dataType)){
            return ResultUtil.ERROR("请选择推荐类型");
        }
        if(StringUtils.isEmpty(placeKey)){
            return ResultUtil.ERROR("请填写推荐位置Key");
        }
        OpGroomPlace groomPlace = opGroomPlaceBiz.queryGroomPlaceByPlaceKey(placeKey);
        String[]  dataId = dataIds.split(",");
        String[]  dataSort = dataSorts.split(",");
        List<OpGroomData> groomDatas = new ArrayList<>();
        for (int i =0;i<dataId.length;i++){
            OpGroomData groomData =new OpGroomData();
            groomData.setDataType(dataType);
            groomData.setPlaceKey(placeKey);
            if(StringUtils.isNotEmpty(dataSort[i])){
                if(Integer.valueOf(dataSort[i])>0){
                    groomData.setSort(Integer.valueOf(dataSort[i]));
                }
            }
            if(StringUtils.isNotEmpty(dataId[i])){
                if(Long.parseLong(dataId[i])>0){
                    groomData.setDataId(new BigDecimal(dataId[i]));
                    groomDatas.add(groomData);
                }
            }
        }
        if(groomDatas.size() > groomNum){
            return ResultUtil.ERROR("推荐数不能超过推荐限制数量，最多可推荐【"+groomNum+"】条数据");
        }
        opGroomDataBiz.saveUpdateGroomData(groomPlace,groomDatas,dataType,placeKey);
        return ResultUtil.SUCCESS("操作成功");
    }


    /**
     * 批量添加推荐
     * @param dataIds 推荐数据Id
     * @param dataType 推荐类型
     * @param placeIds 推荐位置Id
     * @return
     */
    @RequestMapping("/savePlaceDataBatch")
    public Map<String,Object> savePlaceDataBatch(@RequestParam("dataIds")String dataIds,
                                                 @RequestParam("dataType")String dataType,
                                                 @RequestParam("placeIds")String placeIds){
        if(!StringUtils.isNotEmpty(dataIds)){
            return ResultUtil.ERROR("请选择推荐的数据内容");
        }
        if (!StringUtils.isNotEmpty(placeIds)){
            return ResultUtil.ERROR("推荐位置目标不能为空");
        }
        String _placeIds = placeIds.substring(0,placeIds.length()-1);
        String[] dataId =dataIds.split(",");
        int countNum =0;
        for(int i =0;i<dataId.length;i++){
            if(StringUtils.isNotEmpty(dataId[i])){
                countNum++;
            }
        }
        List<OpGroomPlace> groomPlaces =opGroomPlaceBiz.queryGroomPlaceListByIds(_placeIds);
        List<OpGroomData> groomDatas = new ArrayList<>();
        String placeKey = "";
        for(OpGroomPlace groomPlace: groomPlaces){
            if(groomPlace.getGroomNum() < countNum){
                return  ResultUtil.ERROR(groomPlace.getPlaceName()+"超过限制数量，最多可推荐【"+groomPlace.getGroomNum()+"】条数据");
            }
            placeKey+="'"+groomPlace.getPlaceKey()+"',";
            for (int i =0;i<dataId.length;i++){
                OpGroomData groomData = new OpGroomData();
                groomData.setDataType(dataType);
                groomData.setPlaceKey(groomPlace.getPlaceKey());
                if(StringUtils.isNotEmpty(dataId[i])){
                    if(Long.parseLong(dataId[i])>0){
                        groomData.setDataId(new BigDecimal(dataId[i]));
                        groomDatas.add(groomData);
                    }
                }
            }
        }
        if(StringUtils.isNotEmpty(placeKey)){
            placeKey = placeKey.substring(0,placeKey.length()-1);
        }
        opGroomDataBiz.savePlaceDataBatch(groomDatas,dataType,_placeIds,placeKey);
        return ResultUtil.SUCCESS("操作成功");
    }


    /**
     * 删除推荐内容
     * @param dataType
     * @param placeKey
     * @return
     */
    @RequestMapping("/deleteGroomData")
    public Map<String,Object> deleteGroomData(@RequestParam("dataType")String dataType,
                                              @RequestParam("placeKey")String placeKey) {
        opGroomDataBiz.deleteGroomData(dataType,placeKey);
        return ResultUtil.SUCCESS("清空成功");
    }

    /**
     * 查询推荐内容
     * @param groomData
     * @return
     */
    @RequestMapping("/queryGroomDataList")
    public List<OpGroomData> queryGroomDataList(@RequestBody OpGroomData groomData){
        List<OpGroomData> opGroomData = opGroomDataBiz.queryGroomDataList(groomData);
        opGroomDataBiz.setGroomDataByGroomPlace(opGroomData);
        return opGroomData;
    }

    /**
     * 删除推荐数据，通过数据ID串和数据类型
     * @param dataIds 数据ID
     * @param dataType 数据类型
     */
    @RequestMapping("/deleteGroomDataByDataIdAndDataType")
    public void deleteGroomDataByDataIdAndDataType(@RequestParam("dataIds") String dataIds,
                                           @RequestParam("dataType") String dataType){
        opGroomDataBiz.deleteGroomDataByDataIdAndDataType(dataIds,dataType);
    }

    /**
     * 获取H5、App首页课程数据推荐
     * @param placeKey 推荐位置KEY
     * @return List<OpGroomData>
     */
    @RequestMapping("/findAppH5IndexGroomData")
    public List<OpGroomData>  findAppH5IndexGroomData(@RequestParam("placeKey") String placeKey){
        OpGroomData groomData = new OpGroomData();
        groomData.setPlaceKey(placeKey);
        List<OpGroomData> dataListGroom = (List<OpGroomData>) customCacheChannel.get(J2CacheRegion.THIRD_MINUTE_REGION.getKey(), "op_groom_data_" + placeKey).getValue();
        if (ObjectUtils.isEmpty(dataListGroom)) {
            List<OpGroomData> dataList = opGroomDataBiz.queryGroomDataList(groomData);
            opGroomDataBiz.setGroomDataByGroomPlace(dataList);
            customCacheChannel.set(J2CacheRegion.THIRD_MINUTE_REGION.getKey(), "op_groom_data_" + placeKey, dataList);
            return dataList;
        } else {
            return dataListGroom;
        }
    }

}
